<template>
  <div class="index-box d-flex" ref="index">
    <div class="leftBox" :style="{'width': leftTreeShow?'200px':'0px'}">
        <div class="tree-box" style="padding:15px 10px">
          <el-tree :data="treeList" :props="defaultProps"  node-key="typeId"
              highlight-current
              current-node-key="2" default-expand-all @node-click="handleNodeClick"></el-tree>
        </div>
      </div>
      <div class="rightBox">
        <div class="table-box" >
          <div class="search" ref="search">
            <el-form :inline="true" :model="searchForm" size="small" ref="searchForm" @submit.native.prevent
              class="demo-form-inline" id="searchBox">
              <el-form-item prop="name">
                <el-input v-model="searchForm.name" clearable placeholder="请输入标签名称"></el-input>
              </el-form-item>

              <el-form-item class="clearfix">
                <el-button type="primary" @click="handleSearch()" icon="el-icon-search">查询</el-button>
                <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              </el-form-item>

            </el-form>
          </div>
          <div class="actionBtn" ref="actionBtn">
            <div class="actionBtnLeft d-flex align-center">
              <MyLeftTreeRetract :leftTreeShow.sync="leftTreeShow"></MyLeftTreeRetract>
              <el-button type="primary" @click="handleAdd()" size="small" icon="el-icon-plus" v-has="'system:label:add'">添加标签</el-button>
            </div>
            <div class="actionBtnRight">
              <MyRefresh></MyRefresh>
            </div>
          </div>
          <div class="table" ref="table">
            <el-table :data="dataList" style="width: 100%" row-key="id" border :height="tableHeight" default-expand-all stripe>
              <el-table-column type="selection" width="55" ></el-table-column>
              <el-table-column prop="name" label="标签名称"  align="center">
                <template #default="scope">
                  <el-tag  effect="plain" :style="{borderColor:scope.row.color,color: scope.row.color}">{{ scope.row.name }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="name" label="标签颜色"  align="center">
                <template #default="scope">
                  <el-color-picker v-model="scope.row.color" size="small" disabled></el-color-picker>
                </template>
              </el-table-column>
              <el-table-column label="状态"  align="center">
                <template #default="scope">
                  <div class="d-flex justify-center"><MySwitch :value.sync="scope.row.status"  text="启用|禁用" textValue="1|2" @changeStatus="handleChangeStatus(scope.row)" :permission="hasBtn('system:label:state')"></MySwitch></div>
                </template>
              </el-table-column>
              <el-table-column prop="name" label="更新时间"  align="center">
                 <template #default="scope">
                    {{parseTime(scope.row.insertDate)}}
                 </template>
              </el-table-column>
              <el-table-column prop="name" label="操作"  width="180" align="center">
                <template #default="scope">
                    <el-button
                      type="text"
                      icon="el-icon-edit"
                      @click="handleEdit(scope.row)"
                      v-has="'system:label:edit'"
                    >修改</el-button>
                    <el-button
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelt(scope.row)"
                     v-has="'system:label:delt'"
                    >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="page" ref="page">
            <MyPagination
              :total="total"
              :page.sync="searchForm.pageNum"
              :limit.sync="searchForm.pageSize"
              @pagination="dataInfo"
            />

          </div>
        </div>
      </div>

    <!-- 添加或修改标签-->
    <el-dialog :title="dialogTitle" :close-on-click-modal="false" width="600px" v-dialogDrag :visible.sync="addLabelVisible" @close="addLabelVisible = false">
      <el-form  :model="user" label-position="right" :rules="rules" ref="user" class="userModel">
        <el-form-item label="名称:" prop="name" label-width="85px">
          <el-input v-model="user.name" autocomplete="off" placeholder="请输入名称" ></el-input>
        </el-form-item>
        <el-form-item label="关联标签:" prop="type" label-width="85px" >
           <treeselect v-model="user.type" :options="treeList" :normalizer="normalizer" placeholder="关联标签" />
        </el-form-item>
        <el-form-item label="颜色" prop="color" label-width="85px" >
          <el-color-picker v-model="user.color" size="small" style="width:100%"></el-color-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('user')" size="small">重 置</el-button>
        <el-button type="primary" @click="addLabelSubmit('user')" size="small" :loading="btnLoading">保 存</el-button>
      </div>
    </el-dialog>
      <!-- 添加或修改标签-->


  </div>
</template>

<script>
  import common from '@/api/api';
  import { addTag, getTagCategory, getTagList,deleteTag } from '@/api/getData';
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    components: { Treeselect },
    data() {
      return {
        loading: false,
        btnLoading: false,
        leftTreeShow:true,
        searchForm: {
          tagName: undefined,
          typeId :'2',
          pageNum:1,
          pageSize:20
        },
        treeList: [],
        tableHeight:0,//table 高度
        dataList:[],
        total: 0,
        defaultProps: {
           children: 'children',
           label: 'name'
        },
        multipleSelection: '',
        dialogTitle: '',
        addLabelVisible: false,
        user: {
          tagId: undefined,
          name: '',
          type: '',
          color:'#000000'
        },
        rules: {
          name: [{required: true, message: '姓名不能为空',trigger: 'blur'}],
          type: [{required: true, message: '关联分类不能为空',trigger: 'blur'}],
          color: [{required: true, message: '颜色不能为空',trigger: 'blur'}]
        }
      }
    },
    mounted() {
      this.dataInfo();
      this.getTree();
      this.$nextTick(() => {
          this.tableHeight = this.$refs.index.offsetHeight - this.$refs.search.offsetHeight - this.$refs.actionBtn.offsetHeight - this.$refs.page.offsetHeight - 55;
      })
    },
    methods: {

      async getTree() {
        try {
          const result = await getTagCategory();
          if (result.code == 200) {
            this.treeList = result.data;
          } else {
            this.$message.error(result.message);
          }
        } catch (err) {
          console.log(err)
        }
      },
      async dataInfo() {
        try {
          this.loading = true;
          const result = await getTagList(this.searchForm);
          if (result.code == 200) {
            this.total = result.total;
            this.dataList = result.rows;
          } else {
            this.$message.error(result.message);
          }
          this.loading = false;
        } catch (err) {
          this.loading = false;
          console.log(err)
        }
      },
      handleSearch() {
        this.page = 1;
        this.dataInfo();
      },
      resetQuery(){
        this.resetForm('searchForm');
        this.dataInfo();
      },
      /** 转换部门数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.typeId,
          label: node.name,
          children: node.children
        };
      },
      handleAdd() {
        this.addLabelVisible = true;
        this.dialogTitle = '添加标签';
        this.user.tagId = '';
        this.user.name = '';
        this.user.type = '';
        this.user.color = '#000000';
      },
      handleEdit(row) {
        this.dialogTitle = '修改标签';
        this.user.tagId = row.tagId;
        this.user.name = row.name;
        this.user.type = row.type;
        this.user.color = row.color;

        this.addLabelVisible = true;
      },
      handleNodeClick(data){/*树节点点击事件*/
        this.page = 1;
        this.searchForm.typeId  = data.typeId;
        this.dataInfo();
      },
      addLabelSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.btnLoading = true;
            addTag(this.user).then((result) => {
              if (result.code == 200) {
                this.$message.success(result.msg);
                this.addLabelVisible = false;
                this.dataInfo();
              } else {
                this.$message.error(result.message);
              }
              this.btnLoading = false;
            });
          }
        })
      },
      async handleChangeStatus(row) {
        try {
          let data = {
            id: row.id,
            status: row.status
          }
          const result = await getCallRecord(data);
          if (result.success == true) {
            this.$message.success(result.message);
          } else {
            this.$message.error(result.message);
            row.status = !row.status;
          }
        } catch (err) {
          console.log(err)
        }
      },
      handleDelt(row) {
        this.$confirm('您确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteTag({
            tagId : row.tagId
          }).then((result) => {
            if (result.code == 200) {
              this.dataInfo();
              this.$message.success(result.msg);
            } else {
              this.$message.error(result.msg);
            }
          });
        }).catch(() => {
        });
      },

    }
  }
</script>

<style scoped lang="scss">
  .index-box{
    padding:0px!important;
    margin-left:0px;
  }
  .tree-box /deep/ .el-tree-node{
     background: #D8E4DD;
  }
  .tree-box> /deep/ .el-tree>.el-tree-node{
    padding:10px;
  }
  .tree-box> /deep/ .el-tree .is-current{
    background:#fff;
  }
  .table-box{
    padding:15px
  }
  .userModel /deep/ .el-color-picker__trigger{
    width:100%
  }
</style>
